package com.sskj.mine.ui.activity;


import android.os.Bundle;
import android.support.design.widget.BottomSheetDialog;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.sskj.common.base.App;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.LanguageUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.RxBusCode;
import com.sskj.lib.SPConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.util.BottomSheetUtil;
import com.sskj.lib.util.TipUtil;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.presenter.MineLanguageActivityPresenter;

import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Flowable;
import io.reactivex.disposables.Disposable;

@Route(path = RConfig.MINE_LANGUAGE)
public class MineLanguageActivity extends BaseActivity<MineLanguageActivityPresenter> {
    @BindView(R2.id.tvTitle)
    TextView tvTitle;
    @BindView(R2.id.left_img)
    ImageView leftImg;
    @BindView(R2.id.left_tv)
    TextView leftTv;
    @BindView(R2.id.ivBack)
    LinearLayout ivBack;
    @BindView(R2.id.right_tv)
    TextView rightTv;
    @BindView(R2.id.ivRight)
    ImageView ivRight;
    @BindView(R2.id.chinese)
    CheckBox chinese;
    @BindView(R2.id.english)
    CheckBox english;
    @BindView(R2.id.llChinese)
    LinearLayout llChinese;
    @BindView(R2.id.llEnglish)
    LinearLayout llEnglish;
    private BottomSheetDialog bottomSheet;
    private Disposable languageDispo;
    private MaterialDialog tipChinese;
    private MaterialDialog tipEnglish;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_mine_language;
    }

    @Override
    public MineLanguageActivityPresenter getPresenter() {
        return new MineLanguageActivityPresenter();
    }

    @Override
    protected void initView() {
        super.initView();
        tvTitle.setText(App.INSTANCE.getString(R.string.mine_mineLanguageActivity1_j));
        Boolean isChinese = LanguageUtil.isChinese();
        if (isChinese) {
            chinese.setChecked(true);
            english.setChecked(false);
        } else {
            english.setChecked(true);
            chinese.setChecked(false);
        }
        ClickUtil.click(llChinese, () -> {
            if (english.isChecked()) {
                tipChinese = TipUtil.getSureCancelTip(this, App.INSTANCE.getString(R.string.mine_mineLanguageActivity2_j), App.INSTANCE.getString(R.string.mine_mineLanguageActivity3_j), () -> {
                    tipChinese.dismiss();
                    chinese.setChecked(true);
                    english.setChecked(false);
                    changeLanguage(Locale.CHINESE);
                });
            }
        });
        ClickUtil.click(llEnglish, () -> {
            if (chinese.isChecked()) {
                tipEnglish = TipUtil.getSureCancelTip(this, App.INSTANCE.getString(R.string.mine_mineLanguageActivity2_j), App.INSTANCE.getString(R.string.mine_mineLanguageActivity3_j), () -> {
                    tipEnglish.dismiss();
                    english.setChecked(true);
                    chinese.setChecked(false);
                    changeLanguage(Locale.ENGLISH);
                });
            }
        });

        english.setOnClickListener(v -> {

        });
    }
//
//    private void selectLanguage() {
//        languageDispo = Flowable.fromIterable(LanguageUtil.locales)
//                .map(Locale::getDisplayName)
//                .toList()
//                .subscribe((strings, throwable) -> {
//                    bottomSheet = BottomSheetUtil.getBottomSheet(this, null, (recyclerView, position, view) -> {
//                        bottomSheet.dismiss();
//                        changeLanguage(LanguageUtil.getAppLocaleByName(strings.get(position)));
//                    }, strings);
//                    bottomSheet.show();
//                });
//    }

    private void changeLanguage(Locale locale) {

        LanguageUtil.saveLanguageSetting(App.INSTANCE, locale);
        RxBus.getDefault().send(RxBusCode.CHANGE_LANGUAGE);

    }

}
